// WebSocket #########################
import {useWebSocket} from '@vueuse/core'
import {computed, onMounted, onUnmounted, ref} from "vue";
import {useUserStore} from "@/stores/modules/user.js";

const userStore = useUserStore();

const userid = computed(() => userStore.userInfo.userid);

const skUrl = import.meta.env.VITE_WS_API_URL + '/api/user/' + userid.value


const getMessageRun = (ws, event) => {
    // event.data 返回的数据
    let returnResult = JSON.parse(event.data)
    // 触发对应类型的全局事件
    $eventBus.emit(returnResult.type, returnResult.data)
}


const wsInstance = useWebSocket(skUrl, {
    heartbeat: true,
    autoReconnect: true,
    immediate: true,
    onMessage: getMessageRun
})

export const wsStatus = wsInstance.status

